import { http } from "@/utils/http";
import type { AjaxResult } from "./types";
import { baseUrlApi } from "./utils";

// 展示列表查询所有库存
export const listAllStock = () => {
  return http.request<AjaxResult>("get", baseUrlApi("/store/listAll"));
};

/**
 * 分页查询库存列表
 * @param pageNum 页码
 * @param pageSize 每页数量
 * @param condition 查询条件
 * @returns 分页库存列表
 */
export const listStockByPage = (
  pageNum: number,
  pageSize: number,
  condition?: ProductSkusCondition
) => {
  return http.request<AjaxResult>("post", baseUrlApi("/store/page"), {
    params: { pageNum, pageSize },
    data: condition
  });
};

/**
 * 新增库存
 * @param data 库存数据
 * @returns 操作结果
 */
export const addStock = (data: StockForm) => {
  return http.request<AjaxResult>("post", baseUrlApi("/store/add"), { data });
};

/**
 * 更新库存
 * @param data 库存数据
 * @returns 操作结果
 */
export const updateStock = (data: StockForm) => {
  return http.request<AjaxResult>("put", baseUrlApi("/store/update"), { data });
};

/**
 * 删除库存
 * @param skuCode 规格编码
 * @returns 操作结果
 */
export const deleteStock = (skuCode: string) => {
  return http.request<AjaxResult>("delete", baseUrlApi(`/store/${skuCode}`));
};

/**
 * 批量删除库存
 * @param skuCodes 规格编码数组
 * @returns 操作结果
 */
export const deleteStockBatch = (skuCodes: string[]) => {
  return http.request<AjaxResult>(
    "delete",
    baseUrlApi(`/store/delBatch/${skuCodes.join(",")}`)
  );
};

/**
 * 切换上架状态
 * @param skuCode 规格编码
 * @returns 操作结果
 */
export const updateStockStatus = (skuCode: string) => {
  return http.request<AjaxResult>("post", baseUrlApi(`/store/${skuCode}`));
};

// 查询条件接口
interface ProductSkusCondition {
  productSn?: string;
  productName?: string;
  color?: string;
  size?: string;
  status?: number;
}

// 库存列表数据
interface StockDTO {
  skuId: number;
  productSn: string; // 商品编码
  productName: string; // 商品名称
  skuCode: string; // 规格编码
  color: string; // 颜色
  size: string; // 尺码
  stockQuantity: number; // 库存
  price: number; // 价格
  skuUrl: string; // 图片
  status: number; // 上架状态
  updateTime?: string; // 更新时间
}

// 库存表单数据
interface StockForm {
  id?: number;
  productSn: string;
  productName?: string;
  skuCode?: string;
  color: string;
  size: string;
  stockQuantity: number;
  price: number;
  skuUrl: string;
  status: number;
}

export type { ProductSkusCondition, StockDTO, StockForm };
